/**
 * <pre>
 * Title: 		SystemMonitorController.java
 * Author:		linriqing
 * Create:	 	2011-6-24 上午10:23:10
 * Copyright: 	Copyright (c) 2011
 * Company:		Shenzhen Helper
 * <pre>
 */
package com.hengpeng.itfinoss.controller.stm;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hengpeng.common.monitor.Message;
import com.hengpeng.common.monitor.SystemMonitor;
import com.hengpeng.itfinoss.controller.BaseController;

/**
 * <pre>
 * 系统事件监控器
 * </pre>
 * @author linriqing
 * @version 1.0, 2011-6-24
 */
@Controller
@RequestMapping("/systemmonitor")
public class SystemMonitorController extends BaseController
{
	private static long startId = 0;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public String list()
	{
		List<Message> lastMessage = SystemMonitor.getInstance().getLastMessage(startId);
		StringBuffer sb = new StringBuffer();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
		for (Message message : lastMessage)
		{
			sb.append(df.format(message.getOccurTime()) + " [" + message.getId() + "] [EVENT] " + message.getSummary()
					+ "<br />");
			startId = message.getId();
		}
		return sb.toString();
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	@ResponseBody
	public String get(@PathVariable Long id)
	{
		Message message = SystemMonitor.getInstance().getMessage(id);
		if (message != null)
		{
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
			return df.format(message.getOccurTime()) + " [" + message.getId() + "] [EVENT] " + message.getSummary()
					+ "\r\n" + message.getDetail();
		}
		else
		{
			return "";
		}
	}

	@RequestMapping(value = "/entry", method = RequestMethod.GET)
	public String entry()
	{
		return "stm/monitor/monitor";
	}
}
